﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WjAutoConfigApp.Common;
using WjAutoConfigApp.Models;

namespace WjAutoConfigApp
{
    public partial class LanePartPropertySetingForm : Form
    {
        private bool isEdit;
        public LanePartPropertySetingForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //取消
            this.Close();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            WaitWindowRotate.Show();
            //1.将界面配置转换为模型
            LanePartPropertyModel partPropertyModel = new LanePartPropertyModel();
            partPropertyModel.LanePropertyTypy = combLaneProperty.SelectedIndex+1;
            partPropertyModel.Value = (int)combValue.SelectedValue;
            var value = combValue.SelectedValue.ToString();
            //2.获取所有符合条件的像素点
            var matchPoints = CommomMethod.GetMatchPoints(GlobleCache.areaModel.Points);
            //3.修改对应CSV矩阵下的数值
            if (isEdit)
            {
                CSVHelper.EditLanePropertyInfo();
            }
            matchPoints.ForEach(p => CSVHelper.SetCsvPointValue(p, GlobleCache.lanePropertyCsv,combLaneProperty.SelectedIndex+1 , value));
            GlobleCache.areaModel.IsSaved = true;
            GlobleCache.areaModel.PropertyInfoModel = partPropertyModel;
            CommomMethod.SaveFinishArea(GlobleCache.areaModel);
            GlobleCache.areaModel.InitAreaModel();
            WaitWindowRotate.Hide();
            this.Close();
        }

        /// <summary>
        /// 初始化控件数据
        /// </summary>
        private void InitControlData()
        {
            //是否统计区域
            List<CommonContent> CommentData = new List<CommonContent>();
            CommentData.Add(new CommonContent { Code = 0, Value = "否" });
            CommentData.Add(new CommonContent { Code = 1, Value = "是" });
            combValue.DataSource = CommentData;
            combValue.DisplayMember = nameof(CommonContent.Value);
            combValue.ValueMember = nameof(CommonContent.Code);

            //属性值

            List<CommonContent> lanePropertyData = new List<CommonContent>();
            //将枚举转化成list
            lanePropertyData = CommomMethod.EnumToList<LanePropertyType>();
            
            combLaneProperty.DataSource = lanePropertyData;
            combLaneProperty.DisplayMember = nameof(CommonContent.Value);
            combLaneProperty.ValueMember = nameof(CommonContent.Code);
        }

        private void LanePartPropertySetingForm_Load(object sender, EventArgs e)
        {

            InitControlData();
            if (GlobleCache.areaModel.PropertyInfoModel is null) return;

            isEdit = true;
            combLaneProperty.SelectedIndex = GlobleCache.areaModel.PropertyInfoModel.LanePropertyTypy - 1;
            combValue.SelectedIndex = GlobleCache.areaModel.PropertyInfoModel.Value;

        }
    }

}
